package com.job.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.job.common.Constant;
import com.job.common.ResponseResult;
import com.job.common.asserts.MyAssert;
import com.job.common.converter.CorporationFieldConverter;
import com.job.common.converter.PositionFieldConverter;
import com.job.common.enums.ResultCodeEnum;
import com.job.common.exception.ParameterIsNullException;
import com.job.controller.req.PositionSearchVO;
import com.job.dao.PositionDao;
import com.job.mapper.*;
import com.job.pojo.*;
import com.job.service.PositionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

@Service
@Slf4j
public class PositionServiceImpl implements PositionService {

    @Resource
    private PositionMapper positionMapper;

    @Resource
    private PositionInfoMapper positionInfoMapper;

    @Resource
    private CorporationMapper corporationMapper;

    @Resource
    private HrMapper hrMapper;

    @Resource
    private CorporationFieldConverter corporationFieldConverter;

    @Resource
    private PositionFieldConverter positionFieldConverter;

    @Resource
    private PositionDao positionDao;

    @Resource
    private UserMapper userMapper;

    @Override
    public ResponseResult<IPage<Map<String,Object>>> positionSearch(PositionSearchVO search)
            throws ParameterIsNullException {
        MyAssert.objectParameterCheck(search);

        search.setCurrent((search.getCurrent()-1) * search.getCurrent());
        List<Position> positions = positionMapper.searchPositions(search);
        if (CollectionUtils.isEmpty(positions)){
            new ResponseResult<Position>().fail(ResultCodeEnum.POSITION_IS_NULL_FAIL);
        }
        List<Map<String, Object>> list = new ArrayList<>();
        positions.forEach(position -> {
            HashMap<String, Object> map = new HashMap<>();
            map.put("position",positionFieldConverter.PositionPoToVoConverter(position));
            Corporation corporation = corporationMapper.selectById(position.getCorporationId());
            if (!Objects.isNull(corporation)){
                map.put("corporation",corporationFieldConverter.corporationPoToVoConverter(corporation));
            }
            Hr hr = hrMapper.selectById(position.getHrId());
            if (!Objects.isNull(hr)){
                map.put("hr",hr);
                LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(User::getUserType, Constant.HR)
                        .eq(User::getRoleId, hr.getHrId());
                User user = userMapper.selectOne(wrapper);
                map.put("userId",user.getId());
            }
            list.add(map);
        });

        Page<Map<String, Object>> positionPage = new Page<>();
        positionPage.setTotal(positionMapper.searchPositionsCount(search));
        positionPage.setCurrent(search.getCurrent());
        positionPage.setSize(search.getSize());
        positionPage.setRecords(list);
        return new ResponseResult<IPage<Map<String,Object>>>().success(positionPage);
    }



    @Override
    public ResponseResult<Map<String,Object>> positionInfoContext(Integer positionId)
            throws ParameterIsNullException {
        MyAssert.objectParameterCheck(positionId);
        Position position = positionMapper.selectById(positionId);
        if (Objects.isNull(position)){
            log.info("该职位不存在");
            new ResponseResult<Position>().fail(ResultCodeEnum.POSITION_IS_NULL_FAIL);
        }

        PositionInfo positionInfo = positionDao.queryPositionInfo(positionId);
        if (Objects.isNull(positionInfo)){
            log.info("该职位信息不存在");
            new ResponseResult<Position>().fail(ResultCodeEnum.POSITION_INFO_IS_NULL);
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("position",positionFieldConverter.PositionPoToVoConverter(position));
        map.put("positionInfo",positionInfo);
        return new ResponseResult<Map<String,Object>>().success(map);
    }
}
